package cn.tradewin.advisor.persist.services.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;

import cn.tradewin.advisor.persist.mapper.AccountMapper;
import cn.tradewin.advisor.persist.mapper.UsersMapper;
import cn.tradewin.advisor.persist.model.Account;
import cn.tradewin.advisor.persist.model.CodeLabel;
import cn.tradewin.advisor.persist.model.Users;
import cn.tradewin.advisor.persist.services.AccountService;

public class AccountServiceImpl implements AccountService {
	
	private final SqlSessionFactory sqlSessionFactory;
	private final Logger log;
	
	public AccountServiceImpl(final SqlSessionFactory sqlSessionFactory, final Logger log) {
		this.sqlSessionFactory = sqlSessionFactory;
		this.log = log;
	}

	public int addAccount(Account account) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			AccountMapper mapper = session.getMapper(AccountMapper.class);
			int iCount = mapper.insert(account);
			if (iCount < 0) {
				session.rollback();
				return iCount;
			}
			UsersMapper userMapper = session.getMapper(UsersMapper.class);
			Users user = account.getUser();
			user.setLocked("0");
			iCount = userMapper.insert(user);
			if (iCount < 0) {
				session.rollback();
			}
			session.commit();
			return  iCount;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

//	public int delAccount(String id) {
//		SqlSession session = null;
//		try {
//			int iCount = -1;
//			session = sqlSessionFactory.openSession();
//			UsersMapper userMapper = session.getMapper(UsersMapper.class);
//			iCount = userMapper.deleteByPrimaryKey(id);
//			if (iCount < 0) {
//				session.rollback();
//				throw new RuntimeException("failure to delete user[" + id + "] or current user not exist");
//			}
//			AccountMapper accountMapper = session.getMapper(AccountMapper.class);
//			iCount = accountMapper.deleteByPrimaryKey(id);
//			if (iCount < 0) {
//				session.rollback();
//				throw new RuntimeException("failure to delete account[" + id + "] or current account not exist");
//			}
//			session.commit();
//			return  iCount;
//		} catch (Exception e) {
//			if (session != null) {
//				session.rollback();
//			}
//			throw new RuntimeException(e.getMessage());
//		} finally {
//			if (session != null) {
//				session.close();
//			}
//		}
//	}

	public int updAccount(Account account) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			AccountMapper mapper = session.getMapper(AccountMapper.class);
			int count = mapper.updateByPrimaryKey(account);
			if (count < 0) {
				session.rollback();
				throw new RuntimeException("failure update account [" + account.getId() + "]");
			}
			session.commit();
			return count;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	public List<Account> listAllAccounts() {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			AccountMapper mapper = session.getMapper(AccountMapper.class);
			return mapper.selectAllAccounts();
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}
	
	public Account findAccoutByID(String id) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			AccountMapper mapper = session.getMapper(AccountMapper.class);
			return mapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int lockAccount(String id) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			UsersMapper mapper = session.getMapper(UsersMapper.class);
			int count = mapper.lockUser(id);
			if (count < 0) {
				session.rollback();
				throw new RuntimeException("failure lock account [" + id + "]");
			}
			session.commit();
			return count;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public List<CodeLabel> getAccountsOptions() {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			AccountMapper mapper = session.getMapper(AccountMapper.class);
			return mapper.getAccountsOptions();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public List<Account> getAccountsNotInProject(Integer projectId) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			AccountMapper mapper = session.getMapper(AccountMapper.class);
			return mapper.selectAccountsNotInProject(projectId);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public List<Account> getAccountsInProject(Integer projectId) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			AccountMapper mapper = session.getMapper(AccountMapper.class);
			return mapper.selectAccountsInProject(projectId);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}
}
